package com.chuck.controller;

import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Controller
public class SysLoginController {

    @Autowired
    private Producer producer;

    @RequestMapping("/captcha.jpg")
    public void kaptcha(HttpServletResponse response) throws IOException {

        //兼容IE浏览器缓存
        response.setHeader("Cache-Control","no-store,no-catch");

        response.setContentType("image/jpeg");
        //先把验证码的文字生成
        String text = producer.createText();

        //生成图片验证码
        BufferedImage image = producer.createImage(text);

        //保存到session中

        //相应到客户端
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image,"jpg",out);
        out.flush();//清空缓冲区


    }
}
